// import { customAlphabet, nanoid } from 'nanoid';
// const nanoid = customAlphabet('1234567890', 4); // nanoid自定义字符集、位数4位
//$u.guid的文档：https://uiadmin.net/uview-plus/js/guid.html

const formatDate = (date, format = 'date') => {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，所以加1
  const day = String(date.getDate()).padStart(2, '0');
  if (format === 'date') return `${year}-${month}-${day}`;

  const hours = String(date.getHours()).padStart(2, '0');
  const minutes = String(date.getMinutes()).padStart(2, '0');
  const seconds = String(date.getSeconds()).padStart(2, '0');
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

const getRandomDayInPastMonth = (dateStr, months) => {
  // 将时间字符串转换为Date对象
  let date = new Date(dateStr);
  
  // 减去指定的月份数
  date.setMonth(date.getMonth() - months);
  
  // 获取这几个月内的总天数
  let endOfMonth = new Date(date);
  endOfMonth.setMonth(endOfMonth.getMonth() + months);
  let totalDays = Math.round((endOfMonth - date) / (1000 * 60 * 60 * 24)) + 1; // 加1是因为包括起始日当天
  
  // 生成随机天数
  let randomDay = Math.floor(Math.random() * totalDays);
  
  // 设置随机日期
  date.setDate(date.getDate() + randomDay);
  
  return date;
}

const useCommonUtils = () => {
	return {
    formatDate,
    getRandomDayInPastMonth,
		getNumberUuid: (legnth = 5) => Number(uni.$u.guid(legnth, false, 10)),
	}
}

export default useCommonUtils